use {
    crate::model::dapp_block_scan_progress::DAppBlockScanProgress,
    sqlx::{MySql, Pool},
};

impl DAppBlockScanProgress {
    pub async fn select_by_chain(
        chain: &str,
        pool: &Pool<MySql>,
    ) -> anyhow::Result<DAppBlockScanProgress> {
        let query = sqlx::query_as::<_, DAppBlockScanProgress>(
            r#"SELECT * FROM dapp_block_scan_progress where `chain`= ? "#,
        )
        .bind(chain.to_string());

        let row = query.fetch_one(pool).await?;

        Ok(row)
    }

    pub async fn update(pool: &Pool<MySql>, number: u64, id: u64) -> anyhow::Result<bool> {
        let execute_ret =
            sqlx::query(r#"UPDATE dapp_block_scan_progress SET number = ? WHERE id = ?"#)
                .bind(number)
                .bind(id)
                .execute(pool)
                .await?;

        if execute_ret.rows_affected() > 0 {
            Ok(true)
        } else {
            Ok(false)
        }
    }
}
